package com.bigflexlib.ds.op
{
	
	import com.bigflexlib.ds.DataStructureEvent;
	import com.bigflexlib.ds.container.IRContainer;


	public class ContainerAnyOp extends AbstractContainerToElementOp
	{
		


		
		
		/**
		 * 
		 * @param iContainer
		 * @param iValueHandlerFunction
		 * @param emptyValue 
		 * 		The value that should be passed to the value handler when the container becomes empty.
		 * 		It is usually set to undefined or null, but can be anything you like... like the empty string
		 * 
		 */		
		public function ContainerAnyOp( iContainer:IRContainer, iValueHandlerFunction:Function, emptyValue:* = undefined )
		{
			
			super( iContainer, iValueHandlerFunction, emptyValue );
			
		}
		
		
		
		override protected function batch( ):void
		{
			pickAny( );
		}
		
		
		
		private function pickAny( ):void
		{
		
			if ( iContainer.size == 0 )
			{
				updateElement( emptyValue );
				return;
			}
			
			
			// walk one element
			
			iContainer.walk( function( e:*, m:int ):Boolean {
					
					updateElement( e );
					return true;	// we return true to stop walking
							
				} );
		
		}
		

		
		
		
		
		
		override protected function iContainer_change( event:DataStructureEvent ):void
		{
		
			if ( currentElement !== undefined )
			{
				if ( iContainer.contains( currentElement ) ) // we still have the element. don't update
				{
					return;
				}
			}
			
			
			pickAny( );		
		
		}
		
		
		
		
		
		
		
	}
	
}